package com.android.bbczw;
import java.util.HashMap;
import java.util.Map;

import com.android.bbczw.client.HttpClientMgr;
import com.android.bbczw.context.AuthContext;
import com.android.bbczw.holder.AuthContextHolder;
import com.android.bbczw.utils.JSONParseUtils;
import com.android.bbczw.utils.MD5Utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity {
	private Button loginButton;
	private EditText username;
	private EditText password;	
	private TextView failMessage;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);     
        this.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_FULLSCREEN
				, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.login);       
        initLogin();
    }
    
    protected void initLogin() {
    	//��ȡҳ������
    	username = (EditText) this.findViewById(R.id.username_edit);
    	password = (EditText) this.findViewById(R.id.password_edit);
        failMessage = (TextView) this.findViewById(R.id.login_message);
    	loginButton = (Button)this.findViewById(R.id.signin_button);
    	OnClickListener listener = new OnClickListener (){
        	public void onClick(View paramView) {
        		login();
        	}};
    	loginButton.setOnClickListener(listener);
    }
    
    protected void login() {
    	failMessage.setVisibility(0);
    	String userName = username.getText().toString();
    	String userPassword = MD5Utils.toMd5(password.getText().toString().getBytes());
		
		try {			
			//�����������POST���󲢻�ȡ���������صĽ��			
			String postParam = this.getString(R.string.server_url) + this.getString(R.string.login_params);
			Map dataListMap = new HashMap();
			dataListMap.put(this.getString(R.string.login_input_uid), userName);
			dataListMap.put(this.getString(R.string.login_input_password), userPassword);
			dataListMap.put(this.getString(R.string.login_input_isiphone), this.getString(R.string.login_value_isiphone));
			String response = HttpClientMgr.getInstance().execute(postParam, dataListMap, this.getString(R.string.server_chartype));		
			Map<String, String> result = JSONParseUtils.getJSONDataMap(response);
			if(result.containsKey(this.getString(R.string.login_fail_code))) {
				failMessage = (TextView) this.findViewById(R.id.login_message);
				failMessage.setVisibility(1);
				failMessage.setText(result.get(this.getString(R.string.login_fail_message)));
			} else {
				setAuthMessage(result);
				
				//String param = this.getString(R.string.server_url) + this.getString(R.string.testcount);
				//HttpClientMgr.getInstance().execute(param, new HashMap(), this.getString(R.string.server_chartype));	
				
				Intent intent = new Intent();				
				intent.putExtra("uid",result.get("uid"));
				//���Դ���һЩ��Ϣ����һ��activity
				
				Intent it=new Intent(LoginActivity.this, MainActivity.class);
				startActivity(it);
				
				//intent.setClass(LoginActivity.this, GetContactActivity.class);
				//startActivity(intent);   
				finish();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	
    }
    
    private void setAuthMessage(Map<String, String> result) {
    	AuthContext authContext = new AuthContext();
    	authContext.setOauthtoken(result.get(this.getString(R.string.login_succ_oauthtoken)));
    	authContext.setOauthtokensecret(result.get(this.getString(R.string.login_succ_oauthtokensecret)));
    	AuthContextHolder.set(authContext);   	
    }
    
}